<template>
  <div>
    <h1>登录</h1>
    <van-cell-group>
      <van-field clearable v-model="userId" label="" placeholder="用户ID">
        <span slot="text">如何获取</span>
      </van-field>

      <van-field clearable v-model="password" label="" placeholder="密码">
        <span slot="text">如何获取</span>
      </van-field>

      <van-field
        center
        clearable
        v-model="verify"
        label="验证码"
        placeholder=""
      >
        <van-button slot="button" @click="yan()" type="primary" size="middle" plain>
          获取验证码
        </van-button>
      </van-field>
      <van-button @click="login()" type="primary" block>登录</van-button>
    </van-cell-group>

    <div style="margin-top: 410px;">
      <van-row type="flex" justify="space-around">
        <van-col @click="registerLogin()" span="6">还没账号我要注册...</van-col>
      </van-row>
    </div>
    <van-button slot="button" @click="yan2()" type="primary" size="middle" plain>
      wd
    </van-button>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data () {
    return {
      phone: '', // 定义sms变量
      verify: null, // 定义verify变量
      nCustomer:{},
      nid:0,
      user:{},
      userId:null,
      password:'',
    }
  },
  methods: {
    yan() {
      axios.get(`/admin/JWTTest/caehcap?phone=${this.phone}`).then(res=>{
        if (res.data.code==0){
          this.$message.success('发送成功')
        }else {
          this.$message.error(res.data.errorMessage)
        }
      })

    },
    yan2() {
    this.$router.push('/mine')
    },
    registerLogin(){
      this.$router.push('/register')
    },

    login(){
      this.user={
        userId:parseInt(this.userId),
        password:this.password
      }
      axios.post(`/user/user/login`,this.user).then(res=>{
        if(res.data.code==200){
          this.$message.success('登录成功')
          // 将id先存到域中
          this.nid=res.data.data.id
          sessionStorage.setItem('id',JSON.stringify(this.nid))
          localStorage.setItem('user',JSON.stringify(this.nCustomer.data))//存储数据
          console.log(this.nCustomer.data)
          this.$router.push('/home')
        }else {
          this.$message.error('登录失败')
        }
      })
    }
  },
  mounted() {

  }
}
</script>
